feat: auto-create database directory if missing#510
Conversation
WalkthroughThe changes implement automatic directory creation for database initialization while making the database-path configuration optional. The database path now defaults to "/data/tinyauth.db" if not specified, and the directory is created automatically during service initialization. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
internal/service/database_service.go (1)
33-44: Init correctly defaults and prepares the DB path; consider DRY’ing the default and tightening permsThe new
dbPathhandling andMkdirAllcall solve the original startup failure when the directory doesn’t exist and give a sensible fallback when the path is empty. This looks functionally solid.Two non-blocking suggestions:
- Avoid duplicating the default path string:
/data/tinyauth.dbnow lives here and incmd/root.go. Consider centralizing this in a shared constant (e.g., in a config/common package) so future changes don’t drift.- Consider stricter directory permissions: since the DB holds auth data,
0700is usually sufficient and a bit safer than0755in multi-user environments:- dir := filepath.Dir(dbPath) - if err := os.MkdirAll(dir, 0755); err != nil { + dir := filepath.Dir(dbPath) + if err := os.MkdirAll(dir, 0700); err != nil { return fmt.Errorf("failed to create database directory %s: %w", dir, err) }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
cmd/root.go(1 hunks)internal/config/config.go(1 hunks)internal/service/database_service.go(2 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-27T11:02:49.689Z
Learnt from: steveiliop56
Repo: steveiliop56/tinyauth PR: 326
File: internal/service/database_service.go:7-11
Timestamp: 2025-08-27T11:02:49.689Z
Learning: In internal/service/database_service.go, using glebarez/sqlite (modernc) for GORM with sqlite3 (mattn) driver for golang-migrate is a valid pattern that works correctly, despite appearing to be a driver mismatch. The golang-migrate sqlite driver causes panics when used with modernc.org/sqlite backend, so the mixed approach should be maintained.
Applied to files:
internal/service/database_service.go
🧬 Code graph analysis (1)
internal/service/database_service.go (1)
internal/config/config.go (1)
Config(17-46)
🔇 Additional comments (2)
internal/config/config.go (1)
40-40: MakingDatabasePathnon-required aligns with new defaulting behaviorRemoving the
validate:"required"constraint is consistent with providing a default (/data/tinyauth.db) and havingDatabaseService.Inithandle an empty path, so config without an explicit database-path will now start successfully instead of failing validation.cmd/root.go (1)
69-69: Database flag default and description match the new behaviorUsing
/data/tinyauth.dbas the flag default and documenting that the directory will be created if missing is consistent with the new initialization logic and should make Docker usage less surprising.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #510 +/- ##
==========================================
- Coverage 23.62% 23.56% -0.07%
==========================================
Files 36 36
Lines 2239 2245 +6
==========================================
Hits 529 529
- Misses 1673 1679 +6
Partials 37 37 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Problem
When
DATABASE_PATHis not set or points to a non-existent directory, TinyAuth v4 fails to start with a confusing error message ("out of memory (14)"). This is problematic in Docker deployments where users expect the application to work out of the box.Solution
Auto-create the database directory if it doesn't exist and provide a sensible default (
/data/tinyauth.db) whenDATABASE_PATHis empty. This works seamlessly with any Docker volume configuration.Changes
os.MkdirAll(handles multi-level paths)/data/tinyauth.dbwhenDATABASE_PATHis emptyvalidate:"required"to allow flexibilityBehavior
Testing
✅ Code compiles successfully (tested with Docker build)
✅ Follows existing code patterns
✅ Backward compatible
✅ Handles multi-level directory paths
✅ No linter errors
Related
Related to PR #506 which improves error messages when database cannot be opened. This PR fixes the root cause by auto-creating the directory, preventing the error from occurring in the first place.
Summary by CodeRabbit
Release Notes
✏️ Tip: You can customize this high-level summary in your review settings.